package com.example.config;

import com.alipay.api.AlipayConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class AliConfig {

    /**
     * 支付宝证书模式配置
     * 如果证书读取失败，存在resources目录下使用classpath:cert/xxx.crt
     */
    @Bean
    public AlipayConfig getConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        // 设置网关地址
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        // 设置应用ID
        alipayConfig.setAppId("2021005162640269");
        // 设置应用私钥
        alipayConfig.setPrivateKey(
                "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCCweDSVToAwb9hx++wrs5K73f/5O4eudOoRUlSuZiKtNNwrEzNz0NnJisxE5UuCspnflj466OD3PAXvHa6Z80DCIAsmRm5s41YYXcAb3riCYlqpuSKTQSPIkEPu2XRIcuu6HCTs2whmuZJMic3roLcf5pBEWraddV8p1ecwcxGqo0KHdW8iMyYKJPAiAV4Q7V4eUAHJNFnkkoNrFJNtMnyMvYhW9fkkOeS+X5l0N8D3D0//xtlxCGWqdxnAKRK3p1AMHJfGzuDAL1HNoT2xedQORR4AE22v0LZh2xiJ7K0QRWbo/dq2Ywrk7SBdJR79X6crBxhQAaGURC0ILmOi5rLAgMBAAECggEAUyYM3FPb9Z0/IWPt1PHkNxy+t1gR4DIVGT0uGk/xY8sHZe/7SykQ2KgZ8TRduhCck2FyHKBjs7esabur2G+nm9UtBK86/67hJCGedgVuJA0ieFK2pAfD7W0/6Ewnu+x8xLYE2LfQ/DGvXT5qV+s+Rg9SvSvhQhiKcKFNgl5uk0RveBO2Is3q2GBuQLZUxK4IwECaguGbXOeyWsn3onsTlgVLIzHp7aK0B5wrEEEZgTWuBnjot74NXxXeWF9LOSo4TtDAZ3e5n+uzVNSNChvzGoWFmLksOoQ8q7EEKFEaOtCzBhG9aJQ+DARU/k0plIj3YPAA5vZMUJCKwdhTO+xCeQKBgQD4wKxLpn2yQd1IE3ZuRTurPdqOkhSIdJiu1BxsHN2NCKEFPx3RR3JLk0zkR7926Xhbkx+Ca2ENOIB4MPKZdxNGcWpoYNGsi7gKJcSTjFpAqB4f9UWoe+/5AfUlFJcZe5TJeFLlckabCRcln0Ws2d9Gb9Fh0je9n9+Dr/d52saw/QKBgQCGkSJ7Cg75mCSXn9iLv8gtU7eSp4m9A34ZIur5t0VdeYCJmjMVtZUUtuyWTmxCfS7no5Ru9rJWy37SjWH5NQA83ETWNuxC8GVcQAr/Q3tG41FZQCe3fnzoFiGdaQo0H8hwPY3NBDRTvjc8pXeUpv3Tt4gBSWvWxkMweqq+fl+JZwKBgDezVSFfd5f53aXtDcDladYPdKgmF8jyatoDDxY6ZHXXL88v8epR2EsKAdbAJiRnZRcEW7LlfobqVwIS28/3dO56KPyUaX78jkpfaKQtcmw1+QDo+B8hGXwlrxINiUQggxGWCuHr89l1cKRxBLYO10s7cKQ54VdpvDANsBmuHn21AoGAOU4zNdx5thbxdlSO/ixkzdC5+003nfOPzpqNIFzIwF7Ou0qceKFEZHbl17h75/Cxedmyi3KpTccJMhcdYugMOt6+vc7T3EnNkHSBemU9aVnCudRVVZIbFSbfl840ly/1xr6f1W/17LezhDMe8oIO1AtzcsDWmLcDw/BpKAhfHP8CgYAxQj/6TKywOaQOcSumjLRbNMdbIuaYio4p5DzTITeBAhb9I+OGhR3ccrpkcJlYbhoWjFGVRSEhSufwoip7nHUHMhO5FmAG6fKZxrZjF9JUQzEegzv+NqsfmYha+kbGyDmFfJD6utrswBXpBi9xZ97MM59ZPunmrxUf0u8y8BcORw==");
        // 设置请求格式，固定值json
        alipayConfig.setFormat("json");
        // 设置字符集
        alipayConfig.setCharset("UTF-8");
        // 设置签名类型
        alipayConfig.setSignType("RSA2");
        // 设置应用公钥证书路径
        alipayConfig.setAppCertPath("cert/appCertPublicKey_2021005162640269.crt");
        //根目录获取不到使用下列配置
        // alipayConfig.setAppCertPath(resourceToTempFile("classpath:cert/appCertPublicKey_2021005162640269.crt"));
        // 设置支付宝公钥证书路径
        alipayConfig.setAlipayPublicCertPath("cert/alipayCertPublicKey_RSA2.crt");
        // 设置支付宝根证书路径
        alipayConfig.setRootCertPath("cert/alipayRootCert.crt");
        return alipayConfig;
    }

    /**
     * 将classpath资源复制到临时文件并返回临时文件路径
     * 
     * @param classPathResource classpath资源路径
     * @return 临时文件路径
     * @throws IOException 如果发生IO异常
     */

    /*
     * private String resourceToTempFile(String classPathResource) throws
     * IOException {
     * // 移除classpath:前缀
     * String resourcePath = classPathResource.replace("classpath:", "");
     * try (InputStream in =
     * getClass().getClassLoader().getResourceAsStream(resourcePath)) {
     * if (in == null) {
     * throw new IOException("资源未找到: " + resourcePath);
     * }
     * // 创建临时文件
     * Path tempPath = Files.createTempFile("alipay_cert_", ".crt");
     * File tempFile = tempPath.toFile();
     * tempFile.deleteOnExit();
     * // 复制内容到临时文件
     * Files.copy(in, tempPath, StandardCopyOption.REPLACE_EXISTING);
     * return tempFile.getAbsolutePath();
     * }
     * }
     */
}
